<?php
Class Restaurant_model extends CI_Model
{

	function get_restaurants()
	{
		
		$this->db->order_by('restaurants.sequence', 'ASC');
		
		//this will alphabetize them if there is no sequence
		$this->db->order_by('name', 'ASC');
		$result	= $this->db->get('restaurants');
		
		$restaurants	= array();
		foreach($result->result() as $res)
		{
			$restaurants[]	= $this->get_restaurant($res->id);
		}
		
		return $restaurants;
	}
	
	//this is for building a menu
	function get_restaurants_tierd()
	{
		$restaurants	= array();
		$result	= $this->get_restaurants();
		foreach ($result as $restaurant)
		{
			$restaurants[$restaurant->id]['restaurant']	= $restaurant;
		}
		return $restaurants;
	}
	
	function get_restaurants_menu() //TAMBAHAN
	{	
		$restaurants	= $this->db->order_by('id', 'ASC')->get('restaurants')->result();
		$return		= array();
		foreach($restaurants as $r)
		{
			$return[$r->id] = $r->name;
		}
		return $return;
	}
	
	function restaurant_autocomplete($name, $limit)
	{
		return	$this->db->like('name', $name)->get('restaurants', $limit)->result();
	}
	
	function get_restaurant($id)
	{
		return $this->db->get_where('restaurants', array('id'=>$id))->row();
	}
	
	function get_restoran($id) //FUNGSI TAMBAHAN
	{
		return $this->db->where('id', $id)->get('restaurants')->row();
	}
	
	function get_resto($id) //FUNGSI TAMBAHAN
	{
		$restaurants	= array();
		$result	= $this->get_restaurant($id);
		$restaurants[$result->id]['restaurant']	= $result;
		return $restaurants;
	}
	
	function get_restaurants_by_country($id) //FUNGSI TAMBAHAN
	{
		return $this->db->get_where('country_id', $id)->result();
	}
	
	function get_restaurants_by_zone($id) //FUNGSI TAMBAHAN
	{
		return $this->db->get_where('zone_id', $id)->result();
	}
	
	function get_restaurant_products_admin($id)
	{
		$this->db->order_by('sequence', 'ASC');
		$result	= $this->db->get_where('restaurant_products', array('restaurant_id'=>$id));
		$result	= $result->result();
		
		$contents	= array();
		foreach ($result as $product)
		{
			$result2	= $this->db->get_where('products', array('id'=>$product->product_id));
			$result2	= $result2->row();
			
			$contents[]	= $result2;	
		}
		
		return $contents;
	}
	
	function get_restaurant_products($id, $limit, $offset)
	{
		$this->db->order_by('sequence', 'ASC');
		$result	= $this->db->get_where('restaurant_products', array('restaurant_id'=>$id), $limit, $offset);
		$result	= $result->result();
		
		$contents	= array();
		$count		= 1;
		foreach ($result as $product)
		{
			$result2	= $this->db->get_where('products', array('id'=>$product->product_id));
			$result2	= $result2->row();
			
			$contents[$count]	= $result2;
			$count++;
		}
		
		return $contents;
	}
	
	function organize_contents($id, $products)
	{
		//first clear out the contents of the category
		$this->db->where('restaurant_id', $id);
		$this->db->delete('restaurant_products');
		
		//now loop through the products we have and add them in
		$sequence = 0;
		foreach ($products as $product)
		{
			$this->db->insert('restaurant_products', array('restaurant_id'=>$id, 'product_id'=>$product, 'sequence'=>$sequence));
			$sequence++;
		}
	}
	
	function save($restaurant)
	{
		if ($restaurant['id'])
		{
			$this->db->where('id', $restaurant['id']);
			$this->db->update('restaurants', $restaurant);
			
			return $restaurant['id'];
		}
		else
		{
			$this->db->insert('restaurants', $restaurant);
			return $this->db->insert_id();
		}
	}
	
	function delete($id)
	{
		$this->db->where('id', $id);
		$this->db->delete('restaurants');
		
		//delete references to this restaurant in the product to restaurant table
		$this->db->where('restaurant_id', $id);
		$this->db->delete('restaurant_products');
	}
}